// node.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showGift: false, //是否显示进店有礼
    img:[
      'http://img0.imgtn.bdimg.com/it/u=1493616983,785887807&fm=26&gp=0.jpg',
      'http://img4.imgtn.bdimg.com/it/u=2352264204,3365306024&fm=26&gp=0.jpg',
      'http://img5.imgtn.bdimg.com/it/u=3381379707,3093310680&fm=26&gp=0.jpg'
    ],
    item_height:'',
    input_value:'',
    showInput:false, //是否显示输入框
    current:0,//当前的轮播图页数
    ava:'http://img1.imgtn.bdimg.com/it/u=417213285,1308357898&fm=200&gp=0.jpg',//头像url
    title:'你不知道的红酒小知识',//标题
    name:'红豆稀饭',//昵称
    time:'2017-7-20',//发表日期
    collection_num:'',//收藏数
    heart:'456',//赞
  },
  reset(e) {
    this.resetHeight('.img' + e.detail.current, 'item_height')
    this.setData({
      current: e.detail.current
    })
  },
  resetHeight(dome, name) {
    var query = wx.createSelectorQuery()
    let self = this
    query.select(dome).boundingClientRect().exec(res => {
      console.log(res)
      self.setData({
        [name]: res[0].height
      })
    })
  },
  show_input(){
    this.setData({
      showInput: true,
      focus: true,
      reply_id: null      
    })
  },
  input(e) {
    this.setData({
      input_value: e.detail.value
    })
  },
  // 失去焦点，隐藏输入框
  blur(){
    this.setData({
      showInput: false,
    })
  },
  closeMsk(e){
    console.log(e.target.dataset.name)
    if (e.target.dataset.name == 'msk'){
      this.setData({
        showGift : false
      })
    }
  },
  getData(note_id, title, type, collection_num) {
    wx.showLoading({
      title: '正在加载...'
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      self = this
    wx.request({
      url: baseUrl + '/Journal/item',
      data: { note_id, access_token, title, type, collection_num},
      method: 'get',
      success: data => {
        console.log('data', data.data)
        if (data.data.code == 200) {
          self.setData({
            data: data.data.data
          })
          wx.hideLoading()
          // 初始化第一张轮播图的高度
          wx.getSystemInfo({
            success: res => {
              wx.getImageInfo({
                src: data.data.data.images[0],
                success: res2 => {
                  console.log('res2',res2)
                  self.setData({
                    item_height: res.windowWidth / res2.width * res2.height
                  })
                }
              })
            }
          })
          // wx.getSystemInfo({
          //   success: res => {
          //     for (let i = 0; i < data.data.data.images.length;i++){
          //       console.log('img', data.data.data.images[i])
          //       wx.getImageInfo({
          //         src: data.data.data.images[i],
          //         success: res2 => { 
          //           console.log(i, res2)
          //           // var arr = [],
          //           // img_height = Number(res.height);
          //           // arr.push(img_height);
          //           // res2.height = Math.min.apply(Math, arr);
          //           // console.log(res2.height)
          //           // self.setData({
          //           //   item_height: res2.height
          //           // })
          //         }
          //       })
          //     }
          //   }
          // })

          // 进店有礼
          console.log(data.data.data)
          console.log(data.data.data.card)
          let card_length = 0
          for(let i in data.data.data.card){
            card_length ++
          }
          console.log(card_length)
          if (card_length > 0){
            self.setData({
              showGift: true
            })
          }

          /* initEnd */
        }
      }
    })
  },
  onLoad(options) {
    console.log('options', options)
    this.setData({
      note_id: options.note_id,
      type: options.type || 0,
      title: options.title || '',
      star: options.collection_num||''
    })
    // this.getData(options.note_id, '', this.data.type, options.star)
    this.getData(options.note_id, '', this.data.type, options.collection_num)
    this.reset({ detail: { current: 0}})
    this.resetHeight('.img0', 'item_height')
  },
  onShow() {
    this.resetHeight('.img0', 'item_height');
  },
  submit(){
    if (this.data.input_value){
      if (this.data.reply_id){
        this.reply()
      }else{
        this.submit_comment(this.data.data.id, this.data.input_value)
      }
    }else{
      wx.showToast({
        title: '不能发布空评论',
        image: '../../image/error.png'
      })
    }
  },
  submit_comment(note_id, content) {
    wx.showLoading({
      title: '正在评论...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      self = this
    wx.request({
      url: baseUrl + '/Journal/post_comment',
      method: 'post',
      data: { access_token, note_id, content },
      header,
      success: data => {
        console.log(data.data)
        wx.hideLoading()
        if(data.data.code == 200){
          wx.showToast({
            title: '发表成功！',
          })
          self.setData({
            input_value: ''
          })
          setTimeout(()=> {
            self.getData(self.data.note_id)
          },1000)
        }
      }
    })
  },
  // 回复评论
  replyTo(e){
    // console.log(e.currentTarget.dataset.id)
    this.setData({
      focus: true,
      showInput: true,
      reply_id: e.currentTarget.dataset.id
    })
  },
  reply(){
    wx.showLoading({
      title: '正在回复...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      self = this,
      note_id = this.data.data.id,
      reply_id = this.data.reply_id,
      content = this.data.input_value
      wx.request({
        url: baseUrl + '/Journal/post_reply',
        header,
        method: 'post',
        data: { access_token, reply_id, note_id, content},
        success: data => {
          wx.hideLoading()
          if(data.data.code == 200){
            wx.showToast({
              title: '回复评论成功！',
            })
            setTimeout(function(){
              self.getData(self.data.note_id)
            },1000)
          }
        }
      })
  },
  point(){
    wx.showLoading({
      title: '正在' + (!this.data.data.is_point?'':'取消') +'点赞...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      self = this,
      note_id = this.data.data.id
    wx.request({
      url: baseUrl + '/Journal/note_point',
      data: { note_id, access_token },
      method: 'post',
      header,
      success: data => {
        if(data.data.code == 200){
          wx.hideLoading()
          wx.showToast({
            title: data.data.message
          })
          let data_ = self.data.data
          console.log(data.data.data)
          if (data.data.data == 1){
            data_.point_num ++ 
            data_.is_point = 1
          }else{
            data_.point_num -- 
            data_.is_point = 0
          }
          console.log(data_)
          self.setData({
            data: data_
          })
          
        }
      }
    })
  },
  onShareAppMessage(type, collection_num){
    wx.showLoading({
      title: '正在分享...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      self = this,
      note_id = this.data.data.id,
      title = this.data.data.title
    wx.request({
      url: baseUrl + '/Note/share',
      method: 'GET',
      data: { access_token, note_id},
      success: data => {
        console.log(data)
        wx.hideLoading()
        self.setData({
          data: data.data,
        })
        if(data.data.code == 200){

          let baseUrl = getApp().globalData.baseUrl,
            access_token = getApp().globalData.access_token,
            self = this
          wx.request({
            url: baseUrl + '/Journal/item',
            data: { note_id, access_token, title, type, collection_num },
            method: 'get',
            success: data => {
              console.log('data', data.data)
              if (data.data.code == 200) {
                self.setData({
                  data: data.data.data
                })
                wx.hideLoading()
                // 初始化第一张轮播图的高度
                wx.getSystemInfo({
                  success: res => {
                    wx.getImageInfo({
                      src: data.data.data.images[0],
                      success: res2 => {
                        console.log('res2', res2)
                        self.setData({
                          item_height: res.windowWidth / res2.width * res2.height
                        })
                      }
                    })
                  }
                })
                
                // 进店有礼
                console.log(data.data.data)
                console.log(data.data.data.card)
                let card_length = 0
                for (let i in data.data.data.card) {
                  card_length++
                }
                console.log(card_length)
                if (card_length > 0) {
                  self.setData({
                    showGift: true
                  })
                }

                /* initEnd */
              }
            }
          })

          wx.showToast({
            title: '分享成功！',
          })
        }
      }
    })
    return {
      title: title,
      path: `pages/node/node?note_id=${this.data.note_id}&type=1&title=${title}`,
    }
  },
  // 领取优惠券
  getCoupon(){
    wx.showLoading({
      title: '正在领取...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      card_id = this.data.data.card.id,
      header = getApp().globalData.header,
      self = this

    wx.request({
      url: baseUrl + '/Card/get_coupons',
      method: 'GET',
      data: { access_token, card_id},
      success: data =>{
        // console.log(data.data)
        wx.hideLoading()
        if(data.data.code == 200){
          wx.showToast({
            title: '领取成功！'
          })
          self.setData({
            showGift: false
          })
        }
      }
    })
  },
  collect(title, type, collection_num){
    wx.showLoading({
      title: '正在收藏...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      note_id = this.data.data.id,
      header = getApp().globalData.header,
      self = this
    wx.request({
      url: baseUrl + '/Journal/collection',
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: { access_token, note_id },
      success: data => {
        console.log(data)
        wx.hideLoading()
        if (data.data.code == 200) {
          
          let baseUrl = getApp().globalData.baseUrl,
            access_token = getApp().globalData.access_token,
            self = this
          wx.request({
            url: baseUrl + '/Journal/item',
            data: { note_id, access_token, title, type, collection_num },
            method: 'get',
            success: data => {
              console.log('data', data.data)
              if (data.data.code == 200) {
                self.setData({
                  data: data.data.data
                })
                wx.hideLoading()
                // 初始化第一张轮播图的高度
                wx.getSystemInfo({
                  success: res => {
                    wx.getImageInfo({
                      src: data.data.data.images[0],
                      success: res2 => {
                        console.log('res2', res2)
                        self.setData({
                          item_height: res.windowWidth / res2.width * res2.height
                        })
                      }
                    })
                  }
                })

                // 进店有礼
                // console.log(data.data.data)
                // console.log(data.data.data.card)
                // let card_length = 0
                // for (let i in data.data.data.card) {
                //   card_length++
                // }
                // console.log(card_length)
                // if (card_length > 0) {
                //   self.setData({
                //     showGift: true
                //   })
                // }

                /* initEnd */
              }
            }
          })

    
          wx.showToast({
            // title: '收藏成功！'
            title: data.data.message
          })


        }
      }
    })
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  }
})